package com.aabte.springcloudlearn.feigncustomerdemo.controller;

import com.aabte.springcloudlearn.common.beans.User;
import com.aabte.springcloudlearn.feigncustomerdemo.service.UserCustomerFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Daniel
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user")
public class UserCustomerController {

    @Autowired
    private UserCustomerFeignService userCustomerFeignService;

    @GetMapping(value = "/all")
    public List<User> queryAllUser() {
        log.info("Start queryAllUser.");
        List<User> result = userCustomerFeignService.queryAllUser();
        log.info("End queryAllUser. result: {}", result);
        return result;
    }

    @GetMapping("/{id:\\d+}")
    public User queryUser(
            @PathVariable(value = "id") Long userId
    ) {
        log.info("Start queryAllUser.");
        User result = userCustomerFeignService.queryUserById(userId);
        log.info("End queryAllUser. result: {}", result);
        return result;
    }

    @GetMapping
    public List<User> queryUsers(
            @RequestParam(value = "ids", required = false) List<Long> ids
    ) {
        log.info("Start queryUsers. ids: {}", ids);
        List<User> result = userCustomerFeignService.queryUsers(ids);
        log.info("Start queryUsers. ids: {}", ids);
        return result;
    }

    @GetMapping(value = "/header")
    public List<User> queryUserByHeader(
            @RequestHeader(value = "region") String region
    ) {
        log.info("Start queryUserByHeader. region: {}", region);
        List<User> result = userCustomerFeignService.queryUsersByRegion(region);
        log.info("End queryUserByHeader. result: {}", result);
        return result;
    }

    @PostMapping
    public User registerUser(
            @RequestBody @Valid User user
    ) {
        log.info("Start registerUser. user: {}", user);
        User result = userCustomerFeignService.addUser(user);
        log.info("Start registerUser. result: {}", result);
        return result;
    }

    @PutMapping
    public User updateUser(
            @RequestBody @Valid User user
    ) {
        log.info("Start updateUser. user: {}", user);
        User result = userCustomerFeignService.updateUser(user);
        log.info("Start updateUser. result: {}", result);
        return result;
    }

    @DeleteMapping("/{id:\\d+}")
    public User deleteUser(
            @PathVariable(value = "id") Long userId
    ) {
        log.info("Start deleteUser.");
        User result = userCustomerFeignService.deleteUser(userId);
        log.info("End deleteUser. result: {}", result);
        return result;
    }

}
